// pages/myOrder/myOrder.js
import api from '../../utils/request/api'
const util = require('../../utils/util.js');
import {
  userOrder
} from '../../utils/request/apiurl'

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isRequest: false,
    searchGood: {
    	page: 1,
    	size: 10,
      pages: 0
    },
    orderList: [{
        id: 1,
        name: '全部',
        value: "",
        current: true
      },
      {
        id: 2,
        name: '待付款',
        value: 0,
        current: false
      },
      {
        id: 7,
        name: '待发货',
        value: 1,
        current: false
      },
      {
        id: 3,
        name: '待收货',
        value: 2,
        current: false
      },
      {
        id: 5,
        name: '待评价',
        value: 3,
        current: false
      },
      {
        id: 4,
        name: '已完成',
        value: 4,
        current: false
      },
      // {
      //   id: 8,
      //   name: '退款中',
      //   value: 5,
      //   current: false
      // },
      // {
      //   id: 9,
      //   name: '已退款',
      //   value: 6,
      //   current: false
      // },
      // {
      //   id: 10,
      //   name: '退款失败',
      //   value: 7,
      //   current: false
      // },
      {
        id: 6,
        name: '已取消',
        value: 8,
        current: false
      },
    ],
    winHeight: "", //窗口高度
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    goodList: [],
	firstPictureList:[],//多件商品展示图片
	uid:''
  },
  onLoad: function(options) {
	wx.showTabBar();
    var that = this;
    //  高度自适应
    wx.getSystemInfo({
      success: function(res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        // console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });
	var userInfo=wx.getStorageSync('userInfo')
	this.setData({
		uid:userInfo.uid
	})
    //判断当前tag
    let type = options.type
    this.setData({
      currentTab: type * 1,
      goodList: []
    })
    this.getList()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
	  var userInfo=wx.getStorageSync('userInfo')
	this.setData({
		uid:userInfo.uid
	})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
	
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
	
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
  	wx.showNavigationBarLoading() //在标题栏中显示加载
	this.setData({
		 goodList: [],
		 searchGood: {
		 	page: 1,
		 	size: 10,
		   pages: 0
		 },
	})
  	 this.getList()
  	setTimeout(function() {
  		wx.hideNavigationBarLoading() //完成停止加载
  		wx.stopPullDownRefresh() //停止下拉刷新
  	}, 1500);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
	if (!this.data.isRequest && this.data.searchGood.page < this.data.searchGood.pages) {
    this.setData({
      searchGood: {
        page: this.data.searchGood.page + 1,
        size: 10,
        pages: this.data.searchGood.pages
      }
    })
		this.getList()
	}
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  // 滚动切换标签样式
  // switchTab: function(e) {
	 //  // console.log(e)
  //   this.setData({
  //     currentTab: e.detail.current
  //   });
  //   this.checkCor();
  //   this.getList(1)
  // },
  // 点击标题切换当前页时改变样式
  swichNav: function(e) {
    var cur = e.target.dataset.current;
    if (this.data.currentTaB == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur
      })
    }
    this.setData({
      searchGood: {
        page:  this.data.searchGood.page,
        size: this.data.searchGood.size,
        pages:  this.data.searchGood.pages,
      },
      goodList: []
    })
	this.checkCor();
    this.getList(1)
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function() {
    if (this.data.currentTab > 3) {
      this.setData({
        scrollLeft: 45 * this.data.currentTab
      })
      // console.log(this.data.scrollLeft)
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  //获取订单列表
  getList(changeType) {
	  // console.log(changeType)
    let type = this.data.orderList[this.data.currentTab].value
    // let uid =this.data.uid|| wx.getStorageSync('userInfo').uid
    // let uid = "da3bdf61cc1a409cafe573ea389c0e09" //测试uid
    
    api.post(userOrder.list, {
      page: this.data.searchGood.page,
      size: this.data.searchGood.size,
      uid: this.data.uid,
      status: type
    }).then(res => {
      let list = res.rows
      list.map(a => {
		// 多件商品
		// console.log(a)
		if(a.size>1){
			a.firstPicture=a.firstPicture.split(',')
		}
        a.createTime = util.getLocalTime(a.createTime)
        switch (a.pickUp) {
          case 0:
            a.pickUp = "门店自提"
            break;
          case 1:
            a.pickUp = "售货机"
            break;
        }
        return a
      })
      if (changeType === 1) {
        this.setData({
          goodList: list
        })
      } else {
		   // console.log(this.data.goodList)
        this.setData({
          goodList: [...this.data.goodList, ...list]
        })
      }
      let pages = res.total / this.data.searchGood.size
      this.setData({
        searchGood: {
          page: this.data.searchGood.page,
          size: this.data.searchGood.size,
          pages: pages
        },
      })
      // console.log(this.data.firstPictureList)
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none'
      })
    })
  },
  //删除订单
  deleteOrder(e) {
    let id = e.target.dataset['id']
    let that = this
    wx.showModal({
      title: '删除订单',
      content: '是否确认删除这个订单？',
      success: function(res){
        if (res.confirm) {
          api.post(userOrder.deleteOrder, {
            orderNOAll: id
          }).then(res => {
            that.setData({
              goodList: []
            })
            that.getList(1)
          }).catch(err => {
            wx.showToast({
              title: err.message,
              icon: 'none'
            })
          })
        }
      }
    })
  },
  //取消订单
  cancelOrder(e) {
    let id = e.target.dataset['id'] || e.detail.value.input;
    let that = this
    wx.showModal({
      title: '取消订单',
      content: '是否确认取消这个订单？',
      success: function(res) {
        if (res.confirm) {
          api.post(userOrder.cancelOrder, {
            OrderNoAll: id,
            formId: e.detail.formId,
          }).then(res => {
            that.setData({
              goodList: []
            })
			wx.showToast({
			  title: '取消成功',
			  icon: 'none'
			})
            that.getList(1)
          }).catch(err => {
            wx.showToast({
              title: err.message,
              icon: 'none'
            })
          })
        }
      }
    })
  },
  // 确认订单收货
  affirmOrder(e){
	  // console.log(e)formId: e.detail.formId
    console.log("eeee",e)
    // let id = e.currentTarget.dataset.id
    let id = e.detail.value.input
    let that = this
    wx.showModal({
      title: '确认收货',
      content: '是否确定已收到货物？',
      success: function (res) {
        if (res.confirm) {
          api.post(userOrder.affirmOrder, {
            OrderNoAll: id,
            // uid: "da3bdf61cc1a409cafe573ea389c0e09" //测试uid
            uid: that.data.uid||wx.getStorageSync('userInfo').uid,
            formId: e.detail.formId
          }).then(res => {
            that.setData({
              goodList: []
            })
			wx.navigateTo({
				url: '../myOrder/myOrder?type=4'
			})
            that.getList(1)
          }).catch(err => {
            wx.showToast({
              title: err.message,
              icon: 'none'
            })
          })
        }
      }
    })
  },
  // 手动退款
  refundOrder(e) {
	  // console.log(e)
    let orderNum = e.currentTarget.dataset['ordernum']
    let that = this
    wx.showModal({
      title: '手动退款',
      content: '是否确定退款？',
      success: function (res) {
        if (res.confirm) {
          api.post(userOrder.refundOrder, {
            orderNum: orderNum,
            uid: that.data.uid||wx.getStorageSync('userInfo').uid
          }).then(res => {
            that.setData({
              goodList: []
            })
            that.getList(1)
          }).catch(err => {
            wx.showToast({
              title: err.message,
              icon: 'none'
            })
          })
        }
      }
    })
  },
  // 提醒发货
  reminder(e){
	  // console.log(e)
	  let orderNum =e.currentTarget.dataset.ordernum
	  let that = this
	   api.post(userOrder.reminder, {orderNoAll:orderNum,uid:that.data.uid})
	   .then(res => {
		wx.showToast({
		  title: res,
		  icon: 'none'
		})
	   }).catch(err => {
	     wx.showToast({
	       title: err.message,
	       icon: 'none'
	     })
	   })
  },
  footerTap: app.footerTap
})